package com.xinsoft.controller.wms;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmInventoryPlanDTO;
import com.xinsoft.entity.dto.ScmInventoryPlanParam;
import com.xinsoft.entity.vo.ScmInventoryPlanVo;
import com.xinsoft.service.ScmInventoryPlanService;
import com.xinsoft.validator.ValidatorUtils;
import com.xinsoft.validator.group.AddGroup;
import com.xinsoft.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 盘点方案
 * @folder 云供应链/仓库管理/盘点方案
 */
@RestController
@RequestMapping("/scm/inventoryPlan")
public class ScmInventoryPlanController {

    @Autowired
    private ScmInventoryPlanService inventoryPlanService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmInventoryPlanParam param) {
        return Response.succeed(inventoryPlanService.findPageListByParam(param));
    }


    /**
     * 暂存（保存方案/执行盘点）
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmInventoryPlanDTO dto) {
        // 参数校验
        ValidatorUtils.validateEntity(dto, AddGroup.class);
        inventoryPlanService.tempSave(dto);
        return Response.succeed();
    }

    /**
     * 扫码盘点
     */
    @PostMapping("/scanQrInventory")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> scanQrInventory(@RequestBody ScmInventoryPlanDTO dto) {
        ValidatorUtils.validateEntity(dto);
        inventoryPlanService.scanQrInventory(dto);
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmInventoryPlanVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(inventoryPlanService.detail(id));
    }

    /**
     * 盘点结束
     */
    @PostMapping("/over")
    public Response<?> over(@RequestBody ScmInventoryPlanDTO dto){
        // 参数校验
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);
        Integer id = inventoryPlanService.tempSave(dto);
        inventoryPlanService.over(id);
        return Response.succeed();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        ids = ids.stream().distinct().collect(Collectors.toList());
        inventoryPlanService.del(ids);
        return Response.succeed();
    }

    /**
     * 生成盘盈单
     *
     * @param id 盘点方案id
     */
    @GetMapping("/generateInventoryProfit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> generateInventoryProfit(@RequestParam(value = "id") Integer id) {
        inventoryPlanService.generateInventoryProfit(id);
        return Response.succeed();
    }

    /**
     * 生成盘亏单
     *
     * @param id 盘点方案id
     */
    @GetMapping("/generateInventoryLoss")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> generateInventoryLoss(@RequestParam(value = "id") Integer id) {
        inventoryPlanService.generateInventoryLoss(id);
        return Response.succeed();
    }
    //
    // /**
    //  * 导出
    //  */
    // @PostMapping("/export")
    // public Response<?> export(@RequestBody @ApiParam("查询参数") ScmProduceReturnParam param, HttpServletResponse response) throws Exception {
    //     List<ScmProduceReturnPageVo> list = inventoryPlanService.findListByParam(param);
    //     ExcelUtils.exportByEasypoi(list, ScmProduceReturnPageVo.class, "生产领用", "生产领用", response, "生产领用");
    //     return Response.succeed();
    // }
    //
    // /**
    //  * 审核
    //  */
    // @GetMapping("/audit")
    // @Transactional(rollbackFor = Exception.class)
    // public Response<?> audit(@ApiParam(value = "采购入库单id",required = true) @RequestParam("id") Integer id){
    //     inventoryPlanService.audit(id);
    //     return Response.succeed();
    // }
    //
    // /**
    //  * 反审核
    //  */
    // @GetMapping("/reverseAudit")
    // @Transactional(rollbackFor = Exception.class)
    // public Response<?> reverseAudit(@ApiParam(value = "采购入库单id",required = true) @RequestParam("id") Integer id){
    //     inventoryPlanService.reverseAudit(id);
    //     return Response.succeed();
    // }
    //
    // /**
    //  * 保存并审核
    //  */
    // @PostMapping("/saveAudit")
    // @Transactional(rollbackFor = Exception.class)
    // public Response<?> saveAudit(@RequestBody ScmProduceReturnDTO dto){
    //     inventoryPlanService.saveAudit(dto);
    //     return Response.succeed();
    // }
}
